package system.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import com.aliyun.oss.model.ListObjectsV2Result;
import com.baomidou.dynamic.datasource.annotation.DS;
import common.config.datasource.DataSourceConstants;
import common.controller.base.BaseController;
import common.model.po.SysAuthority;
import common.model.po.SysUser;
import common.model.response.base.Result;
import common.util.TreeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import system.config.aliyun.oss.OssService;
import system.config.event.TestEvent;
import system.mapper.TestMapper;
import system.model.converter.SysAuthorityConverter;
import system.model.vo.AuthorityTreeNodeVo;
import system.service.*;
import system.util.BusinessCodeBuilder;
import system.util.MenuTreeBuilder;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.TreeMap;
import java.util.stream.Collectors;

/**
 * 前端控制器
 *
 * @author 米泽鹏
 * @since 2022-02-26
 */
@Api(tags = "system测试")
@RestController
@RequestMapping("/system/test")
public class SystemTestController extends BaseController {

	@Resource
	private TestService testService;
	@Resource
	private TestMapper testMapper;
	@Resource
	private SysUserService userService;
	@Resource
	private SysAuthorityService authorityService;
	@Resource
	private SysBusinessCodeService businessCodeService;
	@Resource
	private SysBusinessCodeDetailService businessCodeDetailService;
	@Resource
	private JavaMailSender javaMailSender;
	@Resource
	private ApplicationEventPublisher publisher;
	@Value("${spring.mail.username}")
	private String mailUsername;
	@Resource
	private OssService ossService;

	@ApiOperation(value = "test01", notes = "test01")
	@PostMapping("/test01")
	public Result<?> test01() {
		return testService.saveUser();
	}

	@DS(DataSourceConstants.INFORMATION_SCHEMA_DATASOURCE)
	@ApiOperation(value = "test02", notes = "test02")
	@PostMapping("/test02")
	public Result<?> test02() {
		return Result.successWithData(testMapper.getAllTableName("satisfactory"));
	}

	@ApiOperation(value = "sendEmail", notes = "测试发邮件")
	@PostMapping("/sendEmail")
	public Result<?> sendEmail() {
		SimpleMailMessage smm = new SimpleMailMessage();
		// 发送人
		smm.setFrom(mailUsername);
		// 收件人
		smm.setTo("mizepeng@hotmail.com");
		// 抄送人
		// smm.setCc("");
		// 邮件主题
		smm.setSubject("SpringBoot发送邮件");
		// 邮件内容
		smm.setText("邮件发送成功啦！");
		// 发送邮件
		javaMailSender.send(smm);
		return Result.success();
	}

	@ApiOperation(value = "sendEvent", notes = "测试事件发布")
	@PostMapping("/sendEvent")
	public Result<?> sendEvent() {
		SysUser user = new SysUser();
		user.setUsername("张三");
		TestEvent event = new TestEvent(user, "事件已发出");
		publisher.publishEvent(event);
		return Result.success();
	}

	@ApiOperation(value = "根据用户id删除用户所有数据", notes = "根据用户id删除用户所有数据")
	@PostMapping("/clearUserData/{userId}")
	public Result<?> clearUserData(@ApiParam(value = "用户id", required = true) @PathVariable Long userId) {
		return userService.clearUserData(userId);
	}

	@ApiOperation(value = "测试TreeUtil", notes = "测试TreeUtil")
	@PostMapping("/test03")
	public Result<?> test03() {
		List<SysAuthority> authorityList = authorityService.list();
		// 构建权限树
		List<AuthorityTreeNodeVo> menuTreeNodeList = SysAuthorityConverter.poListToAuthorityTreeNodeVoList(authorityList);
		List<AuthorityTreeNodeVo> roots = MenuTreeBuilder.buildMenuTree(menuTreeNodeList);
		roots.sort(Comparator.comparing(AuthorityTreeNodeVo::getSort));
		// 设置每个节点的parent属性
		TreeUtil.treeIteratorDown(roots, node -> {
			List<AuthorityTreeNodeVo> children = node.getChildren();
			if (CollUtil.isNotEmpty(children)) {
				children.forEach(child -> child.setParent(node));
			}
		}, AuthorityTreeNodeVo::getChildren);
		// 扁平化树
		Set<AuthorityTreeNodeVo> set = TreeUtil.flattenTree(roots, "children", Collectors.toSet());
		return successWithData(set);
	}

	@ApiOperation(value = "测试新版业务编码", notes = "测试新版业务编码")
	@PostMapping("/test04")
	public Result<?> test04() {
		TreeMap<String, List<Tree<Long>>> treeMap = BusinessCodeBuilder.buildBusinessCodeData(businessCodeService.list(), businessCodeDetailService.list());
		return successWithData(treeMap);
	}

	@ApiOperation(value = "测试查询OSS文件", notes = "测试查询OSS文件")
	@PostMapping("/test05")
	public Result<?> test05() {
		ListObjectsV2Result result = ossService.listObjects("satisfactory", "avatar");
		result.getObjectSummaries().forEach(item -> System.out.println(item.getKey()));
		return successWithData(result);
	}

}
